*Script to rewrite some e-variables in order to to compute marginal effects
*for the NB process

program transform2, eclass
	version 11
	
	tempname a b w V ll constraints Cns oldcommandline clust touse grad gradient N N_clust rank k converged modelbased V_modelbased rules depvar
	local `depvar'="`e(depvar)'"
	scalar `N'=e(N)
	scalar `k'=e(k)
	scalar `ll'=e(ll)
	scalar `converged'=e(converged)
	scalar `rank'=e(rank)
	scalar `N_clust'=e(N_clust)
	matrix `a'=e(b)
	matrix `b'=`a'[1,"``depvar'':"]
	matrix `grad'=e(b)
	matrix `gradient'=`grad'[1,"``depvar'':"]
	matrix `w'=e(V)
	matrix `V'=`w'["``depvar'':","``depvar'':"]
	matrix `modelbased'=e(V_modelbased)
	matrix `V_modelbased'=`modelbased'["``depvar'':","``depvar'':"]
	matrix `rules'=(0,0,0,0)
	local `clust'="`e(clustvar)'"
	local `oldcommandline'=subinstr("`e(cmdline)'","zinb","nbreg",.)
	gen `touse'=e(sample)
	
	
	
	ereturn post `b' `V', esample(`touse') /*deletes all existing e results*/
	
	ereturn matrix gradient `gradient'
	ereturn matrix V_modelbased `V_modelbased'
	ereturn matrix rules `rules'
	ereturn scalar N=`N'
	ereturn scalar k=`k'
	ereturn scalar ll=`ll'
	ereturn scalar converged=`converged'
	ereturn scalar rank=`rank'
	ereturn scalar N_clust=`N_clust'
	*ereturn scalar alpha=8.2086728 /*the alpha dispersion parameter does not matter for marginal effects and delta method*/
		
	ereturn local cmdline "``oldcommandline''"
	ereturn local cmd "nbreg"
	ereturn local predict "nbreg_p"
	ereturn local estat_cmd "logit_estat"
	ereturn local marginsnotok "stdp DBeta DEviance DX2 DDeviance Hat Number Residuals RStandard SCore"
	ereturn local title "Logistic regression"
	ereturn local user "nbreg_lf"
	ereturn local chi2type "Wald"
	ereturn local opt "moptimize"
	ereturn local vcetype "Robust"
	ereturn local clustvar "``clust''"
	ereturn local vce "cluster"
	ereturn local title "Negative binomial regression"
	ereturn local crittype "log pseudolikelihood"
	ereturn local ml_method "e2"
	ereturn local singularHmethod "m-marquardt"
	ereturn local technique "nr"
	ereturn local which "max"
	ereturn local properties "b V"
	ereturn local depvar "``depvar''"
	ereturn local disperse "mean"
	ereturn local diparm_opt2 "noprob"
	ereturn local diparm1 "lnalpha, exp label("alpha")"

	display "-----------------------------------------------------------------------------------------"
	display "Transform success: It now looks as if a NB2 model (cmd nbreg) has been estimated with the"
	display "parameters of the nbreg process of the zero inflated model."
	
end


















